<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页开关灯</title>
    <style>
        .open{
            margin: 200px;
        }
    </style>
</head>
<body>
    <button class="open">关灯</button>
    <script>
        var btn=document.querySelector('.open');
        var bodyEle=document.body;
        var flag=0;
        btn.onclick=function(){
            if(flag==1){
                bodyEle.style.backgroundColor='#fff';
                btn.innerHTML='关灯';
                flag=0;
            }else if(flag==0){
                bodyEle.style.backgroundColor='black';
                btn.innerHTML='开灯';
                flag=1;
            }
        }
    </script>
</body>
</html>